package com.pshcp.patient.tool;

import com.pshcp.common.enums.RegTypeEnum;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.DeptGroup;
import com.pshcp.patient.domain.dept.vo.DeptTreeVO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 科室工具类
 */
public class DeptTools {

    public static List<DeptTreeVO> buildOutTree(Integer regType, List<DeptGroup> deptGroups){
        List<DeptTreeVO> deptTreeVOS = new ArrayList<>();
        if(regType == null || RegTypeEnum.NORMAL.getCode() == regType.intValue()){
            if(CollectionUtils.isEmpty(deptGroups)){
                DeptTreeVO treeVO = new DeptTreeVO();
                treeVO.setDeptId(RegTypeEnum.NORMAL.getCode() + "");
                treeVO.setName(RegTypeEnum.NORMAL.getDesc());
                deptTreeVOS.add(treeVO);
            }else {
                deptGroups.forEach(d->{
                    DeptTreeVO treeVO = new DeptTreeVO();
                    treeVO.setDeptId(d.getId() + "");
                    treeVO.setName(d.getGroupName());
                    deptTreeVOS.add(treeVO);
                });
            }
        }
        if(regType == null || RegTypeEnum.TODAY.getCode() == regType.intValue()){
            DeptTreeVO treeVO = new DeptTreeVO();
            treeVO.setDeptId(RegTypeEnum.TODAY.getCode() + "");
            treeVO.setName(RegTypeEnum.TODAY.getDesc());
            deptTreeVOS.add(treeVO);
        }
        return deptTreeVOS;
    }

    public static List<DeptTreeVO> buildList2Tree(List<Dept> depts){
        if(CollectionUtils.isEmpty(depts)){
            return Collections.emptyList();
        }
        List<DeptTreeVO> subDepts = new ArrayList<>();
        depts.forEach(d -> {
            DeptTreeVO vo = new DeptTreeVO();
            vo.setDeptId(d.getHisDeptId());
            vo.setName(d.getHisDeptName());
            subDepts.add(vo);
        });
        return subDepts;
    }

    public static List<DeptTreeVO> buildModel2List(List<Dept> depts){
        if(ObjectUtils.isEmpty(depts)){
            return Collections.emptyList();
        }
        List<DeptTreeVO> treeVOS = new ArrayList<>();
        depts.forEach(d -> {
            DeptTreeVO deptTreeVO = new DeptTreeVO();
            deptTreeVO.setDeptId(d.getHisDeptId());
            deptTreeVO.setName(d.getHisDeptName());
            treeVOS.add(deptTreeVO);
        });
        return treeVOS;
    }

}
